define(function(require, exports, module) {
	"use strict";
	require("mui");
	require("jquery");
	require("common");
	var chioceTab = require("chioceTab");
	var abpUse = require("abpinit");
	var template = require("template");
	var abp = abpUse.abpstart;
	var muiTips = require("muiTips"); //封装的弹框
	var keyWordText = ""; //关键字搜索
	//调用页面权限控制接口
	var sourceType = abp.sourceType; //获取权限类型接口数据
	var authorityInit = require("operatAuthority"); //调用封装的权限方法
	var authorityData = sessionStorage.getItem('operatAuthorityData'); //获取权限数据
	var authorityIdList = authorityInit.getJurisdictionId(authorityData); //权限一级判断
	var authoritySELL_OUTPUT = authorityInit.getJurisdiction(authorityData, sourceType.SELL_OUTPUT); //销售出库权限判断
	//返回清楚审批中心订单缓存
	//返回清除缓存
	var commonBack = require("commonBack");
	//不在钉钉中返回
	mui("body").on("tap", "header>button", function() {
		commonBack.secondBack();
	});
	//钉钉返回
	var dingTalkNav = require('dd_config');
	dingTalkNav.LeftNavBar(commonBack.secondBack);
	//ad
	dingTalkNav.leftAndroid(commonBack.secondBack);

	function getRightFun() {
		document.location.href = "../../src/newSell/newSellOutOrder.html?dd_nav_bgcolor=FFFF943E";
	}
	if(authorityIdList.indexOf(sourceType.INFO_GOODS) === -1) {
		$('.mui-content').addClass('initContentTips');
		$('.mui-content > .tipsConInit').html("您没有权限查看订单资料哦!");
		$("#addGoods").hide();
		$("ul.pop-tab > li").addClass('mui-disabled');
		dingTalkNav.RightNavBar(false, "新增", "default");
	} else {
		if(authoritySELL_OUTPUT.indexOf(2) === -1) {
			$("header > a").remove();
			dingTalkNav.RightNavBar(false, "新增", "default");
		} else {
			$("#addOrder").show();
			dingTalkNav.RightNavBar(true, "新增", getRightFun);
		}
		if(authoritySELL_OUTPUT.indexOf(1) === -1) {
			$('.mui-content > .tipsConInit').html("您没有权限查看订单资料哦!");
			$('.mui-content').addClass("initContentTips");
			$("ul.pop-tab > li").addClass('mui-disabled');
		} else {
			var getSearchItem = require("getSearchItem"); //调用封装搜索
			getSearchItem.getSearchItem("#chioce-search"); //添加搜索效果
			getSearchItem.noSearchTap("ul.pop-tab .pop-tab-titleitem"); //点击其他遮罩层关闭搜索
			$("body").on("keyup", "#searchText", function() { //搜索框事件
				keyWordText = $("#searchText").val();
				getOutSalesList("OutSellList", "#SellOutList", 'searchContentTips', false);
			});

			//调用日期筛选传值
			require("getSearchDateItem");
			//调用状态筛选值
			require("recordStatusID");
			var startDate = ''; //开始时间
			var endDate = ''; //结束时间
			var strStatusId = []; //id筛选
			strStatusId[0] = '';
			var pageIndex = 1; //当前分页
			var dataState = true; //判断如何渲染页面，true替换加载，false加载在末尾
			var pageState = 10; //判断有多少页，调用接口再动态改

			var sourceId = 0;
			if(sessionStorage.getItem('inSalesID')) {
				sourceId = Number(sessionStorage.getItem('inSalesID'));
			}

			mui.init({
				swipeBack: true, //启用右滑关闭功能
				pullRefresh: { //配置分页下拉刷新
					container: '#pullrefresh',
					down: {
						callback: pulldownRefresh
					},
					up: {
						contentrefresh: '正在加载...',
						callback: pullupRefresh
					}
				}
			});

			//按时间筛选数据
			mui('body').on('tap', '.pop-tab-con1 li', function() {
				pageIndex = 1;
				startDate = $(this).attr('data-statedate');
				endDate = $(this).attr('data-endDate');
				dataState = true;
				getOutSalesList("OutSellList", "#SellOutList", 'searchContentTips', false);
				mui('#pullrefresh').pullRefresh().refresh(true); //重新启用页面上滑加载
			});
			//按状态筛选数据
			mui('body').on('tap', '.pop-tab-con2 li', function() {
				pageIndex = 1;
				strStatusId[0] = $(this).attr('data-StatusId');
				dataState = true;
				getOutSalesList("OutSellList", "#SellOutList", 'searchContentTips', false);
				mui('#pullrefresh').pullRefresh().refresh(true); //重新启用页面上滑加载
			});

			mui("body").on("tap", ".pop-tab-conitem li", function() {
				chioceTab.muiSure();
			});

			//详情页参数
			mui('body').on('tap', '#pullrefresh li', function() {
				sessionStorage.setItem('salesOutId', $(this).attr('id'));

				var strStatusId = $(this).children("a").children("div").children("div").children(".statusId").attr("data-statusid");
				sessionStorage.setItem("strStatusId", strStatusId);
				if($(this).attr('data-SourceType') === '' || $(this).attr('data-SourceType') === undefined) {

				} else {
					sessionStorage.setItem('outSSourceType', $(this).attr('data-SourceType'));
				}
				if($(this).attr('data-SourceID') === '' || $(this).attr('data-SourceID') === undefined) {

				} else {
					sessionStorage.setItem('outSSourceID', $(this).attr('data-SourceID'));
				}
			});

			getOutSalesList("OutSellList", "#SellOutList", 'initContentTips', true);

		}
	}

	function getOutSalesList(templateId, mainId, contentTips, isChioce) {
		var option = {};
		var dataModel = {
			"sourceId": sourceId,
			"serial": keyWordText,
			"companyId": [],
			"busUserId": [],
			"statusId": strStatusId,
			"start": startDate,
			"end": endDate,
			"orderBy": "",
			"pageIndex": pageIndex,
			"pageSize": 20
		};

		abp.services.app.sellOutput.getPage(dataModel, option)
			.done(function(data) {
				var totalCountNum = data.totalCount; //获取数据列表条数
				pageState = Math.ceil(totalCountNum / 20); //动态修改数据列表页数
				if(totalCountNum < 1) { //动态改变无数据提示
					if(isChioce === true) {
						$('.mui-content > .tipsConInit').html("还没有订单资料哦!");
					}
					$('.mui-content').addClass(contentTips);
					$('.mui-pull-top-pocket').css('display', 'none');
					mui('#pullrefresh').pullRefresh().endPullupToRefresh(false);
				} else {
					$('.mui-content').removeClass(contentTips);
				}
				var items = data.items;
				/*截取时间*/
				$.each(items, function(i, v) {
					var serialDate = v.serialDate.substring(0, 10);
					var dataCreated = v.dataCreated.substring(0, 10);
					v.dataCreated = dataCreated;
					v.serialDate = serialDate;
				});
				var html = template(templateId, data);
				sessionStorage.removeItem("inSalesID");
				if(dataState === true) { //判断是页面刷新还是上滑追加数据
					$(mainId).html(html);
				} else {
					$(mainId).append(html);
				}

				var statusIdColor = require("statusIdColor");
				statusIdColor($(".statusId"));
			})
			.fail(function(data) {

			});
	}

	//下滑加载具体业务实现
	function pulldownRefresh() {
		pageIndex = 1;
		dataState = true;
		pageState = 10;
		setTimeout(function() {
			getOutSalesList("OutSellList", "#SellOutList", 'searchContentTips', false);
			mui('#pullrefresh').pullRefresh().endPulldownToRefresh(); //刷新完成
			mui('#pullrefresh').pullRefresh().refresh(true); //重新启用页面上滑加载
		}, 500);
	}
	//上滑加载具体业务实现
	function pullupRefresh() {
		pageIndex++;
		dataState = false;
		setTimeout(function() {
			mui('#pullrefresh').pullRefresh().endPullupToRefresh((pageState < pageIndex)); //参数为true代表没有更多数据了。
			getOutSalesList("OutSellList", "#SellOutList", 'searchContentTips', false);
		}, 500);
	}

});